package net.adetech.cjg.innerservice.controller;


import com.alibaba.fastjson.JSONObject;
import net.adetech.cjg.innerservice.entity.Apk;
import net.adetech.cjg.innerservice.entity.Zdpc;
import net.adetech.cjg.innerservice.mapper.ApkDao;
import net.adetech.cjg.innerservice.mapper.ZdpcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class ApkController {

    @Autowired
    private HttpServletRequest request;


    @Autowired
    ApkDao apkDao;


    @Autowired
    ZdpcDao zdpcDao;

    @RequestMapping(value = "/cjg/apkversion",produces = "application/json")
    public String getNewVersion()
    {
        String ip = getClientIp();
        List<Apk> apkList = apkDao.getUpdateApkListByIp(ip);
        Zdpc zdpc = zdpcDao.getZdpcByIp(ip);
        JSONObject resultJson = new JSONObject();
        resultJson.put("version",0);
        if (apkList!=null && apkList.size()>0)
        {
            resultJson.put("version",apkList.get(0).getBbh());
        }
        resultJson.put("clientName",zdpc.getZdkhmc());
        return resultJson.toJSONString();
    }



    private String getClientIp() {
        String remoteAddr = "";

        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }
        return remoteAddr;
    }
}
